package io.renren.sf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.sf.entity.BookRightEntity;
import io.renren.sf.entity.BookRoleEntity;
import io.renren.sf.entity.CbAppreciationPictureEntity;
import io.renren.sf.entity.CbExpandEditionEntity;
import io.renren.sf.service.*;
import io.renren.sf.vo.BookEditionInfoVo;
import io.renren.sf.vo.BookSaveRoleVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.util.*;


/**
 * 
 *
 * @author tongxiaowu
 * @email 15755337992@163.com
 * @date 2021-08-07 14:31:51
 */
@RestController
@RequestMapping("sf/bookrole")
public class BookRoleController {
    @Autowired
    private BookRoleService bookRoleService;

    @Autowired
    private CbBookService cbBookService;

    @Autowired
    private CbStiffBookService cbStiffBookService;

    @Autowired
    private BookRightService bookRightService;

    @Autowired
    private CbExpandEditionService cbExpandEditionService;

    /**
     * 角色列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bookRoleService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 删除角色
     */
    @SysLog("删除角色")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] roleIds){
        bookRoleService.deleteBatch(roleIds);

        return R.ok();
    }

    @GetMapping("/getAllBook")
    public R getAllBook(){
        List<BookEditionInfoVo> allBookEditionList = cbBookService.getAllBookEditionInfoVo();
        List<BookEditionInfoVo> allStiffBookEditionList = cbStiffBookService.getAllBookEditionInfoVo();
        List<CbExpandEditionEntity> expandEditionList = cbExpandEditionService.list(new QueryWrapper<CbExpandEditionEntity>().eq("add_type", 0));
        List<CbExpandEditionEntity> expandEditionStiffList = cbExpandEditionService.list(new QueryWrapper<CbExpandEditionEntity>().eq("add_type", 1));
        return R.ok().put("allBookEditionList",allBookEditionList).put("allStiffBookEditionList",allStiffBookEditionList).put("expandEditionList",expandEditionList).put("expandEditionStiffList",expandEditionStiffList);
    }

    @PostMapping("/save")
    public R save(@RequestBody BookSaveRoleVo bookSaveRoleVo){
        //保存角色记录
        BookRoleEntity bookRoleEntity = new BookRoleEntity();
        bookRoleEntity.setCreatetime(new Date());
        bookRoleEntity.setRemark(bookSaveRoleVo.getRemark());
        bookRoleEntity.setRoleName(bookSaveRoleVo.getRoleName());
        bookRoleService.save(bookRoleEntity);
        //保存书权限记录
        ArrayList<BookRightEntity> bookRights = new ArrayList<>();
        List<Long> bookIds = bookSaveRoleVo.getBookIds();
        BookRightEntity bookRightEntity = null;
        if(bookIds != null){
            for (Long bookId : bookIds) {
                bookRightEntity = new BookRightEntity();
                bookRightEntity.setRightType(0);
                bookRightEntity.setRid(bookRoleEntity.getId());
                bookRightEntity.setBid(bookId);
                bookRights.add(bookRightEntity);
            }
        }
        List<Long> stiffBookIds = bookSaveRoleVo.getStiffBookIds();
        if(stiffBookIds != null){
            for (Long stiffBookId : stiffBookIds) {
                bookRightEntity = new BookRightEntity();
                bookRightEntity.setRightType(1);
                bookRightEntity.setRid(bookRoleEntity.getId());
                bookRightEntity.setSid(stiffBookId);
                bookRights.add(bookRightEntity);
            }
        }
        List<Long> bookExpandIds = bookSaveRoleVo.getBookExpandIds();
        if(bookExpandIds != null){
            for (Long bookExpandId : bookExpandIds) {
                bookRightEntity = new BookRightEntity();
                bookRightEntity.setRightType(2);
                bookRightEntity.setRid(bookRoleEntity.getId());
                bookRightEntity.setEid(bookExpandId);
                bookRights.add(bookRightEntity);
            }
        }
        List<Long> stiffBookExpandIds = bookSaveRoleVo.getStiffBookExpandIds();
        if(bookExpandIds != null){
            for (Long stiffBookExpandId : stiffBookExpandIds) {
                bookRightEntity = new BookRightEntity();
                bookRightEntity.setRightType(3);
                bookRightEntity.setRid(bookRoleEntity.getId());
                bookRightEntity.setEid(stiffBookExpandId);
                bookRights.add(bookRightEntity);
            }
        }
        bookRightService.saveBatch(bookRights);
        return R.ok();
    }

    @PostMapping("/update")
    public R update(@RequestBody BookSaveRoleVo bookSaveRoleVo){
        //更新角色记录
        BookRoleEntity bookRoleEntity = new BookRoleEntity();
        bookRoleEntity.setId(bookSaveRoleVo.getId());
        bookRoleEntity.setRemark(bookSaveRoleVo.getRemark());
        bookRoleEntity.setRoleName(bookSaveRoleVo.getRoleName());
        bookRoleService.updateById(bookRoleEntity);
        //删除所有权限记录
        bookRightService.remove(new QueryWrapper<BookRightEntity>().eq("rid",bookSaveRoleVo.getId()));
        //保存书权限记录
        ArrayList<BookRightEntity> bookRights = new ArrayList<>();
        List<Long> bookIds = bookSaveRoleVo.getBookIds();
        BookRightEntity bookRightEntity = null;
        if(bookIds.size() > 0){
            for (Long bookId : bookIds) {
                bookRightEntity = new BookRightEntity();
                bookRightEntity.setRightType(0);
                bookRightEntity.setRid(bookRoleEntity.getId());
                bookRightEntity.setBid(bookId);
                bookRights.add(bookRightEntity);
            }
        }
        List<Long> stiffBookIds = bookSaveRoleVo.getStiffBookIds();
        if(stiffBookIds.size() > 0){
            for (Long stiffBookId : stiffBookIds) {
                bookRightEntity = new BookRightEntity();
                bookRightEntity.setRightType(1);
                bookRightEntity.setRid(bookRoleEntity.getId());
                bookRightEntity.setSid(stiffBookId);
                bookRights.add(bookRightEntity);
            }
        }
        List<Long> bookExpandIds = bookSaveRoleVo.getBookExpandIds();
        if(bookExpandIds.size() > 0){
            for (Long bookExpandId : bookExpandIds) {
                bookRightEntity = new BookRightEntity();
                bookRightEntity.setRightType(2);
                bookRightEntity.setRid(bookRoleEntity.getId());
                bookRightEntity.setEid(bookExpandId);
                bookRights.add(bookRightEntity);
            }
        }
        List<Long> stiffBookExpandIds = bookSaveRoleVo.getStiffBookExpandIds();
        if(bookExpandIds.size() > 0){
            for (Long stiffBookExpandId : stiffBookExpandIds) {
                bookRightEntity = new BookRightEntity();
                bookRightEntity.setRightType(3);
                bookRightEntity.setRid(bookRoleEntity.getId());
                bookRightEntity.setEid(stiffBookExpandId);
                bookRights.add(bookRightEntity);
            }
        }
        bookRightService.saveBatch(bookRights);
        return R.ok();
    }
    /**
     * 角色列表
     */
    @GetMapping("/select")
    public R select(){
        List<BookRoleEntity> list = bookRoleService.list();
        return R.ok().put("list", list);
    }
}
